package com.lc.cms.service.impl;

import com.lc.cms.dao.IDepartmentDao;
import com.lc.cms.dto.DepartmentPermissionDto;
import com.lc.cms.model.DepartmentModel;
import com.lc.cms.service.IDepartmentService;
import com.lc.cms.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Log4j2
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    private final IDepartmentDao departmentDao;

    @Autowired
    public DepartmentServiceImpl(IDepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    @Override
    public ResponseResult create(DepartmentModel department) {
        try {
            int createResult = departmentDao.create(department);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(DepartmentModel department) {
        try {
            int updateResult = departmentDao.update(department);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult delete(int departmentId) {
        try {
            int deleteUserDepartmentResult = departmentDao.deleteUserDepartmentByDepartmentId(departmentId);
            if (deleteUserDepartmentResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteDepartmentRoleResult = departmentDao.deleteDepartmentRoleByDepartmentId(departmentId);
            if (deleteDepartmentRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteDepartmentPermissionResult = departmentDao.deleteDepartmentPermissionByDepartmentId(departmentId);
            if (deleteDepartmentPermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteResult = departmentDao.delete(departmentId);
            if (deleteResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult updateDepartmentPermission(DepartmentPermissionDto department) {
        try {
            int deleteDepartmentPermissionResult = departmentDao.deleteDepartmentPermissionByDepartmentId(department.getDepartmentId());
            if (deleteDepartmentPermissionResult == 0) {

                return ResponseResult.OPERATION_FAILED;
            }
            int createDepartmentPermissionResult = departmentDao.createDepartmentPermission(department);
            if (createDepartmentPermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
